// 业务异常处理类

import 'package:dio/dio.dart';
import 'package:flutter_module/common/channel/extremely_rich_channel.dart';

import '../ApiResponse.dart';
import 'AppException.dart';
import 'ExceptionConstant.dart';

class AppExceptionHandle {
  static final AppExceptionHandle _instance = AppExceptionHandle._internal();
  // 单例模式
  factory AppExceptionHandle() => _instance;

  AppExceptionHandle._internal() {}

  void init() {}

  AppException handelError({
    required Response<dynamic> response,
    required ApiResponse api,
  }) {
    var err_code = -1;
    var err_msg = "服务器开小差了";
    if (api.message != null) {
      err_msg = api.message!;
    }
    if (api.code != null) {
      err_code = api.code!;
    }
    if (api.message != null) {
      err_msg = api.message!;
    }
    if (api.code == BUSINESS_ERROR_CODE) {
      // 803
    } else if (api.code == LOGIN_AUTH_FAILED_CODE) {
      //1008 登录失效 UnauthorisedException
      //发起通知原生app清除登录态以及调起登录页
      ExtremelyRichChannel.instance.sendToNativeConfig();
      return UnauthorisedException(err_code, err_msg);
    }
    return AppException(err_code, err_msg);
  }
}
